package cn.exrick.controller;

import cn.exrick.pojo.TbFeedbackCat;
import cn.exrick.pojo.common.DataTablesResult;
import cn.exrick.pojo.common.Result;
import cn.exrick.service.FeedbackCatService;
import cn.exrick.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 反馈分类管理
 */
@RestController
@Api(description= "反馈分类管理")
public class FeedbackCatController {

    @Autowired
    private FeedbackCatService feedbackCatService;

    @RequestMapping(value = "/feedback/cat/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取反馈分类列表")
    public DataTablesResult getList(){
        return feedbackCatService.getFeedbackCatList();
    }

    @RequestMapping(value = "/feedback/cat/count",method = RequestMethod.GET)
    @ApiOperation(value = "统计反馈分类数")
    public Result<Object> getCount(){
        Integer result = feedbackCatService.countFeedbackCat();
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/feedback/cat/getAll",method = RequestMethod.GET)
    @ApiOperation(value = "获取所有反馈分类")
    public Result<List<TbFeedbackCat>> getAll(){
        List<TbFeedbackCat> list = feedbackCatService.getAll();
        return new ResultUtil<List<TbFeedbackCat>>().setData(list);
    }

    @RequestMapping(value = "/feedback/cat/checkName",method = RequestMethod.GET)
    @ApiOperation(value = "判断反馈分类是否已存在")
    public boolean checkName(String name){
        return feedbackCatService.getByName(name) == null;
    }

    @RequestMapping(value = "/feedback/cat/checkName/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "判断反馈分类是否已存在")
    public boolean checkNameForEdit(@PathVariable int id, String name){
        return feedbackCatService.checkNameForEdit(id, name);
    }

    @RequestMapping(value = "/feedback/cat/add",method = RequestMethod.POST)
    @ApiOperation(value = "添加反馈分类")
    public Result<Object> addFeedbackCat(@ModelAttribute TbFeedbackCat tbFeedbackCat){
        feedbackCatService.addFeedbackCat(tbFeedbackCat);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/feedback/cat/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新反馈分类")
    public Result<Object> updateFeedbackCat(@ModelAttribute TbFeedbackCat tbFeedbackCat){
        feedbackCatService.updateFeedbackCat(tbFeedbackCat);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/feedback/cat/delete/{ids}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除反馈分类")
    public Result<Object> delRole(@PathVariable int[] ids){
        for(int id : ids){
            int result = feedbackCatService.deleteFeedbackCat(id);
            if(result == 0){
                return new ResultUtil<Object>().setErrorMsg("id为"+id+"的分类被使用中，不能删除！");
            }
        }
        return new ResultUtil<Object>().setData(null);
    }

}
